<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * @Class: Category
 * @Author: Nguyen Tran Vu
 */

class Document extends Zend_Db_Table_Abstract {
    protected $_name = 'cms_document';
    protected $_primary = 'document_id';

    public function getAllDocuments(&$pageCount, $page = 0, $keyword = "") {
        $pageSize = Zend_Registry::get('page_size');
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        $select = $tb->select()->from('cms_document', array('totalrows' => 'COUNT(*)'));
        if($keyword != "") {
            $select = $select->where('name LIKE ?', "%$keyword%")
                    ->orWhere('description LIKE ?', "%$keyword%");
        }
        
        $totalRows = $tb->fetchOne($select);        
        $pageCount = $totalRows / $pageSize;


        $select = $this->select();
        if($keyword != "") {
            $select = $select->where('name LIKE ?', "%$keyword%")
                    ->orWhere('description LIKE ?', "%$keyword%");
        }

        if($page == 0) {            
            $rows = $tb->fetchAll($select->limit($pageSize));
        } else {
            $rows = $tb->fetchAll($select->limit($pageSize,($page-1)*$pageSize));
        }

        return $rows;
    }

    public function getAllDocumentsByCategory(&$pageCount, $id, $page = 1, $keyword = "") {
        $tb = Zend_Db_Table::getDefaultAdapter();
        $searchCondition = '';
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        if($keyword != "") {
            $searchCondition = "AND (d.name LIKE '%$keyword%' OR d.description LIKE '%$keyword%')";
        }
        
        $query = 'select COUNT(*)'
                . ' from cms_document as d'
                . ' left join cms_catalog_document as c'
                . ' on d.document_id = c.document_id'
                . ' where c.catalog_id = ' . $id
                . $searchCondition;

        
        $pageSize = Zend_Registry::get('page_size');
        $totalRows = $tb->fetchOne($query);
        $pageCount = $totalRows / $pageSize;
      
        $query = 'select d.document_id document_id, d.create_date create_date, d.name name, d.description description, d.view view, d.download download'
                . ' from cms_document as d'
                . ' left join cms_catalog_document as c'
                . ' on d.document_id = c.document_id'
                . ' where c.catalog_id = ' . $id
                . $searchCondition
                . ' limit ' . ($page - 1) * $pageSize .', '.$pageSize;
           
        $rows = $tb->fetchAll($query);
        
        return $rows;
    }

    public function getDocument($id) {
        $documentRowset = $this->fetchAll($this->select()->where('document_id = ?', $id));
        $document = $documentRowset->current();
        return $document;
    }

    public function updateViewCount($id) {
        $row = $this->fetchRow($this->select()->where('document_id = ?', $id));        
        $row->view = $row->view + 1;
        $row->save();
    }

    public function updateDownloadCount($id) {
        $row = $this->fetchRow($this->select()->where('document_id = ?', $id));
        $row->download = $row->download + 1;
        $row->save();
    }
}

?>
